﻿using biaoqian;
using FastReport.DevComponents.DotNetBar.Controls;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.DynamicData;
using System.Web.Mvc;
using Wkol.Models;

namespace Wkol.Controllers
{
    public class FreightController : Controller
    {
        // GET: Freight
        LDhuoyunEntities db = new LDhuoyunEntities();
        Class1 one = new Class1();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Freight_management(int state=0,int id=0,int id_2=0)
        {
            var indec = db.CYDW.ToList();
            ViewBag.cydw = indec;
            if (state == 1)
            {
                var temp = db.CLSJ.ToList();
                var temp2 = db.CXSJ.ToList();
                if (id != 0)
                {
                    temp = temp.Where(x => x.cl_ss == id).ToList();
                }
                var index = from a in temp
                            select new { name = a.CYDW.cy_name, a.cl_cp, a.cl_zz, a.cl_rj, a.cl_dw,a.cl_id ,a.cl_ss, cx_sh2=a.CXSJ.cx_sh};
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state==2)
            {
                var list = db.YFJG.Where(x =>x.jw_cx == id).ToList();
                var temp = from a in list
                           select new { a.jw_id, a.jw_bian, a.jw_jt, a.jw_jg };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                var list = db.CXSJ.Where(x => x.cx_ss == id).ToList();
                var temp = from a in list
                           select new { a.cx_id, a.cx_name, a.cx_ss };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 4)
            {
                var temp = db.CLSJ.ToList();
                if (id != 0)
                {
                    temp = temp.Where(x => x.cl_ss == id).ToList();
                }
                if (id_2 != 0)
                {
                    temp = temp.Where(x => x.cl_cx == id_2).ToList();
                }
                var index = from a in temp
                            select new { name = a.CYDW.cy_name, a.cl_cp, a.cl_zz, a.cl_rj, a.cl_dw, a.cl_id, a.cl_ss };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }

        public ActionResult Calculate_freight(string combs, string cs, int state=0,int ss=0,string cph=null ,int gls=0,double zl=0,double tj=0  )
        {
            var indec = db.CYDW.ToList();
            ViewBag.cydw = indec;
            //显示和查找
            if (state == 1)
            {
                DateTime now = DateTime.Now;
                string currentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                K3CloudApi client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "SAL_DELIVERYNOTICE";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FBillNo,F_SMGS_BaseProperty7,FReceiveAddress,F_SMGS_KM,F_SMGS_Integer4,FMaterialName,F_SMGS_YFKG,F_SMGS_Decimal1";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var ZBLx = combs;
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FBillNo like'%" + ZBLx + "%'and FBillTypeID ='193822715afc48aa9fa6d6beca7700ab'and FPlanDeliveryDate >'" + currentDateTime + "'and FCancelStatus='A'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 120,
                    OrderString = "FBillNo",
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                /* JsonSerializerSettings settings = new JsonSerializerSettings();
                 string jsongStr = JsonConvert.SerializeObject(result, settings);*/
                //转换成标准文档并遍历
                DataTable dataTable = one.ERP_fh();
                foreach (var item in result)
                {
                    dataTable.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]);
                }
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(dataTable, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                DateTime now = DateTime.Now;
                string currentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                K3CloudApi client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "SAL_DELIVERYNOTICE";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FBillNo,F_SMGS_BaseProperty7,FReceiveAddress,F_SMGS_KM,F_SMGS_Integer4,FMaterialName,F_SMGS_YFKG,F_SMGS_Decimal1";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var ZBLx = combs;
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FBillNo like'%" + ZBLx + "%'and FBillTypeID ='193822715afc48aa9fa6d6beca7700ab'and FPlanDeliveryDate >'" + currentDateTime + "'and FCancelStatus='A'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 120,
                    OrderString = "FBillNo",
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                /* JsonSerializerSettings settings = new JsonSerializerSettings();
                 string jsongStr = JsonConvert.SerializeObject(result, settings);*/
                //转换成标准文档并遍历
                DataTable dataTable = one.ERP_fh();
                foreach (var item in result)
                {
                    dataTable.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]);
                }
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(dataTable, settings);

                return Content(jsongStr, "application/json");
            }
            else if(state==3)
            {
                double cx = (int)db.CLSJ.Where(x => x.cl_cp == cph).FirstOrDefault().cl_cx;
                List<YFJG> yf = db.YFJG.Where(a => a.jw_cx == cx&&a.jw_gs==ss&& a.jw_bian == "阶梯").ToList();
                YFJG yf_qb = db.YFJG.Where(a => a.jw_cx == cx && a.jw_gs == ss && a.jw_bian == "起步").FirstOrDefault();
                YFJG yf_zg = db.YFJG.Where(a => a.jw_cx == cx && a.jw_gs == ss && a.jw_bian == "最高").FirstOrDefault();
                double number = 0;
                double jsglsd = 0;
                double num = 0;
                if (gls > double.Parse(yf_qb.jw_jt))
                {
                    number += double.Parse(yf_qb.jw_jg.ToString());
                    jsglsd += double.Parse(yf_qb.jw_jt);

                    if (gls> double.Parse(yf_zg.jw_jt))
                    {
                        foreach (YFJG ffjg in yf)
                        {

                            num = (double.Parse(ffjg.jw_jt)) - jsglsd;
                            number += num * double.Parse(ffjg.jw_jg.ToString());
                            jsglsd += num;
                        }

                        number  = double.Parse(yf_zg.jw_jt) * double.Parse(yf_zg.jw_jg.ToString());
                        return Content(number.ToString());
                    }
                    else
                    {
                        foreach (YFJG ffjg in yf)
                        {
                            if (gls >= double.Parse(ffjg.jw_jt))
                            {
                                num = double.Parse(ffjg.jw_jt) - jsglsd;
                                number += num * double.Parse(ffjg.jw_jg.ToString());
                                jsglsd += num;
                            }
                            else
                            {
                                return Content(number.ToString());
                            }
                        }
                    }
                }
                else
                {
                    number += double.Parse(yf_qb.jw_jg.ToString());
                    return Content(number.ToString());
                }
                
            }
            else if (state == 4)
            {
                double temp = 100000;
                double temp_cpg = 0;
                string ccc= cs;
                char[] delimiterChars = { ' ' };
                string[] words = ccc.Split(delimiterChars);
                string cphh = "";
                double number = 0;
                double number2 = 0;
                double jsglsd = 0;
                double num = 0;
                double zaizhong2 = 0;
                double rongji2 = 0;
                double biaoj = 0;
                int xxx = 0;
                for (double zl2 = zl,tj2 = tj; zl2 >= 0 || tj2 >= 0; zl2 -= zaizhong2,tj2 -= rongji2)
                {
                    temp = 1000000;
                    foreach (string cls in words)
                    {
                        if (cls != "")
                        {
                            number = 0;
                            jsglsd = 0;
                            
                            CLSJ cl = db.CLSJ.Where(x => x.cl_cp == cls).FirstOrDefault();
                            double zaizhong = double.Parse(cl.cl_zz);
                            double rongji = double.Parse(cl.cl_rj);
                            List<YFJG> yf = db.YFJG.Where(a => a.jw_cx == cl.cl_cx && a.jw_gs == cl.cl_ss && a.jw_bian == "阶梯").ToList();
                            YFJG yf_qb = db.YFJG.Where(a => a.jw_cx == cl.cl_cx && a.jw_gs == cl.cl_ss && a.jw_bian == "起步").FirstOrDefault();
                            YFJG yf_zg = db.YFJG.Where(a => a.jw_cx == cl.cl_cx && a.jw_gs == cl.cl_ss && a.jw_bian == "最高").FirstOrDefault();

                            if (gls > double.Parse(yf_qb.jw_jt))
                            {
                                number += double.Parse(yf_qb.jw_jg.ToString());
                                jsglsd += double.Parse(yf_qb.jw_jt);

                                if (gls > double.Parse(yf_zg.jw_jt))
                                {
                                    foreach (YFJG ffjg in yf)
                                    {

                                        num = (int.Parse(ffjg.jw_jt)) - jsglsd;
                                        number += num * double.Parse(ffjg.jw_jg.ToString());
                                        jsglsd += num;
                                    }
                                    number  = int.Parse(yf_zg.jw_jt) * int.Parse(yf_zg.jw_jg.ToString());
                                }
                                else
                                {
                                    foreach (YFJG ffjg in yf)
                                    {
                                        if (gls >= int.Parse(ffjg.jw_jt))
                                        {
                                            num = int.Parse(ffjg.jw_jt) - jsglsd;
                                            number += num * double.Parse(ffjg.jw_jg.ToString());
                                            jsglsd += num;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                number += int.Parse(yf_qb.jw_jg.ToString());
                            }
                            if(tj2>= rongji)
                            {
                                if (number / double.Parse(cl.cl_rj) < temp)
                                {
                                    temp = number / int.Parse(cl.cl_zz);
                                    temp_cpg = cl.cl_id;
                                    zaizhong2 = zaizhong;
                                    rongji2 = rongji;
                                    biaoj = number;
                                }
                            }
                            else
                            {
                                if (number / tj2 < temp)
                                {
                                    temp = number / tj2;
                                    temp_cpg = cl.cl_id;
                                    zaizhong2 = zaizhong;
                                    rongji2 = rongji;
                                    biaoj = number;
                                }
                            }
                            
                        }
                    }
                    number2 += biaoj;
                    xxx++;
                }

                string number3 = number2.ToString()+"/"+xxx.ToString();
                return Content(number3);
            }
            return View();
        }

        public ActionResult SH( int state=0)
        {
            if (state == 1)
            {
                var index = db.YDXX.ToList();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);
                return Content(jsongStr, "application/json");
            }
            return View();

        }


        public ActionResult Expense_audit(int id,int state = 0)
        {
            Session["gs_id"] = id;
            if (state == 1)
            {
                List<YFSH> index = db.YFSH.Where(x => x.ss_ss == id && x.ss_sh == "审核").ToList();
                index=index.GroupBy(d => new { d.ss_cx }).Select(a => a.OrderByDescending(b => b.ss_id).ElementAt(0)).ToList();
                var temp = from a in index
                           select new { gs_name = a.CYDW.cy_name, c_name = a.CXSJ.cx_name,a.ss_cx,a.ss_bian};
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 2)
            {
                var index = db.YFSH.Where(x => x.ss_cx == id && x.ss_sh == "审核").ToList();
                var temp = from a in index
                           select new { a.ss_id, a.ss_jg, a.ss_jt, gs_name = a.CYDW.cy_name, c_name = a.CXSJ.cx_name, a.ss_cx, a.ss_bian};
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 3)
            {
                SHZT yfsh = db.SHZT.Where(x => x.sh_id == id).FirstOrDefault();
                yfsh.sh_zt = "不通过";


                List<YFSH> temp = db.YFSH.Where(x => x.ss_ss == int.Parse(yfsh.sh_zhuantiao) && x.ss_sh == "审核").ToList();
                if (temp.Count != 0)
                {
                    foreach (YFSH ss in temp)
                    {
                        ss.ss_sh = "结束";
                        db.SaveChanges();
                    }
                }

                int temp2 = db.SaveChanges();
                return Content(temp2.ToString());
            }
            if (state == 4)
            {
                SHZT yfsh = db.SHZT.Where(x => x.sh_id == id).FirstOrDefault();
                yfsh.sh_zt = "通过";

                List<YFSH> temp = db.YFSH.Where(x => x.ss_ss == int.Parse(yfsh.sh_zhuantiao) && x.ss_sh == "审核").ToList();
                if (temp.Count != 0)
                {
                    foreach (YFSH ss in temp)
                    {
                        ss.ss_sh = "结束";
                        db.SaveChanges();
                    }
                }

                int temp2 = db.SaveChanges();
                return Content(temp2.ToString());
            }
            return View();

        }


        public ActionResult Carrier_audit(int state=0,int name=0)
        {
            if (state == 1)
            {
                var list = db.SHZT.Where(x=>x.sh_zt== "待审核").ToList();
                var temp = from a in list
                           select new { a.sh_ren, a.sh_zt, a.sh_id, a.sh_lx, a.sh_zhuantiao };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                string list = db.CYDW.Where(x => x.cy_id == name).FirstOrDefault().cy_name;
                return Content(list);
            }
            return View();
        }

        public ActionResult ADD_SH_YF(List<YFJG> yf)
        {
            if (yf == null)
            {
                yf = new List<YFJG>();
            }
            foreach(YFJG y in yf)
            {
                List<YFJG> yf2 = db.YFJG.Where(x => x.jw_cx == y.jw_cx && x.jw_gs == y.jw_gs).ToList();
                if (yf2.Count != 0)
                {
                    foreach (YFJG f in yf2)
                    {
                        YFJG yFJG = db.YFJG.Where(x => x.jw_id == f.jw_id).FirstOrDefault();
                        db.YFJG.Remove(yFJG);
                        db.SaveChanges();
                    }
                }
                
            }
            foreach (YFJG f2 in yf)
            {
                db.YFJG.Add(f2);
            }
            int sun = db.SaveChanges();
            return Json(sun);
        }



        public ActionResult ADD_YD(List<YDXX> YD)
        {
            string chuangj = "";
            if (YD == null)
            {
                YD = new List<YDXX>();
            }
            foreach (YDXX b in YD)
            {
                int temp = db.YDXX.Where(x => x.yd_fhdh.Contains(b.yd_fhdh)).Count();
                if (temp == 0)
                {
                    db.YDXX.Add(b);
                    int index = db.SaveChanges();
                    if (index == 1)
                    {
                        chuangj = "添加成功";
                    }
                    else
                    {
                        chuangj = "添加失败";
                    }
                    
                }
                else
                {
                    chuangj = "重复添加";
                }
            }
            
            return Json(chuangj);
        }
        
        public ActionResult add_cy(List<CYDW> cydw)
        {
            if (cydw == null)
            {
                cydw = new List<CYDW>();
            }
            foreach (CYDW b in cydw)
            {
                db.CYDW.Add(b);
            }
            int sun = db.SaveChanges();

            return Json(sun);
        }

        public ActionResult add_cx(List<CXSJ> cxsj)
        {
            int sun = 0;
            if (cxsj == null)
            {
                cxsj = new List<CXSJ>();
            }
            foreach (CXSJ b in cxsj)
            {
                db.CXSJ.Add(b);
                sun +=db.SaveChanges();
            }
            return Json(sun);
        }

        public ActionResult add_shyf(List<YFSH> cxsj)
        {
            int sun = 0;
            if (cxsj == null)
            {
                cxsj = new List<YFSH>();
            }
            foreach (YFSH b in cxsj)
            {
                db.YFSH.Add(b);
                sun += db.SaveChanges();
            }
            return Json(sun);
        }

        public ActionResult add_shzt(List<SHZT> cxsj)
        {
            int sun = 0;
            if (cxsj == null)
            {
                cxsj = new List<SHZT>();
            }
            foreach (SHZT b in cxsj)
            {
                db.SHZT.Add(b);
                sun += db.SaveChanges();
            }
            return Json(sun);
        }

        public ActionResult add_clsj(List<CLSJ> cxsj)
        {
            int sun = 0;
            if (cxsj == null)
            {
                cxsj = new List<CLSJ>();
            }
            foreach (CLSJ b in cxsj)
            {
                db.CLSJ.Add(b);
                sun += db.SaveChanges();
            }
            return Json(sun);
        }
    }
}